<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 14:08
 */

namespace app\admin\logic;
use app\admin\logic\code\ErrorCode;
use app\common\model\SiteTeam;
use app\common\model\User;
use app\common\model\Site;
use app\common\model\Admin;
use app\vehicle\model\Volunteer;
use app\vehicle\model\Practice;
use app\vehicle\logic\TplLogic;
class SiteTeamLogic extends BaseLogic
{

    private $SiteTeamModel;

    public function __construct()
    {
        parent::__construct();
        $this->SiteTeamModel = new SiteTeam();
    }

    public function cateList($tempWhere, $nowPage){
        if ($tempWhere['name']){
            $where[] = ['name', 'like', "%".$tempWhere['name']."%"];
        }
        if (is_numeric($tempWhere['status']) === true){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        if (is_numeric($tempWhere['is_add']) === true){
            $where[] = ['is_add', '=', $tempWhere['is_add']];
        }
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        if($site_id){
            $where[] = ['sid', 'eq', $site_id];
        }
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        if($oid){
            $where[] = ['id', '=', $oid];
        }
        $count = SiteTeam::where($where)->count();
        if ($count <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        } else{
            $cateList = SiteTeam::with(['site','street'])->where($where)->page($nowPage,$tempWhere['limit'])->order('v_number desc,id desc')->select();
            foreach($cateList as $k=>$v){
                $cateList[$k]['numbers'] = Volunteer::where('oid','eq',$v['id'])->count();
                $cateList[$k]['a_num'] = Practice::where('oid','eq',$v['id'])->count();
                $cateList[$k]['service_num'] = Volunteer::alias('as v')
                    ->join(['tr_user'=>'u'],'v.user_id=u.id')
                    ->where('v.oid','eq',$v['id'])->sum('duration1')+Volunteer::alias('as v')
                        ->join(['tr_user'=>'u'],'v.user_id=u.id')
                        ->where('v.oid','eq',$v['id'])->sum('duration');
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $cateList, $count);
        }
    }
    /**
     * 分类信息
     */
    public function cateInfo($id){
        $cateInfo = $this->SiteTeamModel->get($id);
        if ($cateInfo){
            return ajaxArr(ErrorCode::$SUCCESS, 'success',$cateInfo);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '分类信息为空');
        }
    }

    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        $info = SiteTeam::get($data['id']);

        switch ($act){
            case 'add':
                $data['create_time'] = time();
                $res = $this->SiteTeamModel->allowField(true)->save($data);
                $data['status'] && User::where('id','eq',$data['user_id'])->update(['team_admin'=>$this->SiteTeamModel->id]);

                $insert['name'] = $data['name'];
                $insert['nick_name'] = $data['name'];
                $insert['salt'] = rand(1000, 9999);
                $insert['password'] = pwdMd5('123456', $insert['salt']);
                $insert['create_time'] = time();
                $insert['status'] = 1;
                $insert['role_id'] = 6;
                $insert['oid'] = $this->SiteTeamModel->id;
                Admin::insert($insert);

                break;
            case 'edit':
                if(!$data['oid']){
                    return ajaxArr(ErrorCode::$OPERATIONERROR, '请选择归属服务队');
                }
                User::where('team_admin','eq',$data['id'])->update(['team_admin'=>0]);
                if($data['status']==1 ){
                    if($data['user_id']) {
                        User::where('id', 'eq', $data['user_id'])->update(['team_admin' => $data['id']]);
                    }
                    if(!Admin::where('name','eq',$info['name'])->count()) {
                        $insert['name'] = $info['name'];
                        $insert['nick_name'] = $info['name'];
                        $insert['salt'] = rand(1000, 9999);
                        $insert['password'] = pwdMd5('123456', $insert['salt']);
                        $insert['create_time'] = time();
                        $insert['status'] = 1;
                        $insert['role_id'] = 6;
                        $insert['oid'] = $data['id'];
                        Admin::insert($insert);
                    }else{
                        Admin::where('oid','eq',$data['id'])->update(['name'=>$data['name']]);
                    }
                }
                $openid = User::where('id','eq',$data['user_id'])->value('open_id');
                if($data['status']==1){
                    $openid && TplLogic::examineTpl($openid,'服务队申请','审核通过');
                }else if($data['status']==2){
                    $openid && TplLogic::examineTpl($openid,'服务队申请','审核不通过');
                }
                if($data['type_id']==1){
                    unset($data['sid']);
                }else{
                    unset($data['oid']);
                }
                $res = $this->SiteTeamModel->allowField(true)->save($data,['id' => $data['id']]);
                break;
            case 'bing':
                if(!$data['sid']){
                    return ajaxArr(ErrorCode::$OPERATIONERROR, '请选择归属站点');
                }
                $res = Site::where('id','=',$data['id'])->update(['sid'=>$data['sid']]);
                break;
            case 'del':
                $cateInfo = SiteTeam::get($data['id']);
                User::where('id','eq',$cateInfo['user_id'])->update(['team_admin'=>0]);
                Admin::where('oid','eq',$data['id'])->delete();
                $res = $cateInfo->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

    public function getAllCate(){
        $where[] = ['status', '=', 1];
        $cateList = $this->SiteTeamModel->where($where)->select();
        if ($cateList){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $cateList);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }
}